getStudentInfo()
function getStudentInfo(){
    Ajax({
        url:'http://localhost/h5/php/ckqueryall.php',
        success(res){
            // console.log(res);
           
            // console.log(tbody); 
            sessionStorage.setItem('allckInfo',res);//将获取到的所有数据存储到sessionStorage里面
            res = JSON.parse(res)
            let result = res.slice(0,9)//；一页显示6条数据
            // console.log(result);
            renderckInfo(result)       
          
        }
    })
}

//实现删除和修改
deleteOrUpdate()     //调用      delete：删除的意思  or:或   update:修改
     function deleteOrUpdate(){
      const tbody = document.querySelector("tbody")
      tbody.addEventListener("click",function(e){     //监听tbody 点击事件    发生改变
           e = e || window.event;
          //  console.log(e.target.dataset.o);
          //  console.log(e.target.dataset.id);
           //删除操作
           if(e.target.dataset.o == "del"){
            //防止误删
            const flag = window.confirm("确定要删除吗？")
            if(flag){
              Ajax({
                url:"http://localhost/h5/php/ckDeleteByid.php",
                data:{id:e.target.dataset.id},
                success(res){
                  // console.log(res);
                  location.reload();
                  getStudentInfo()
                }
              })
            }
           };
          // 修改的编辑操作 
          if(e.target.dataset.o == 'update'){
            console.log(e.target.dataset.id)
            Ajax({   
              url:"http://localhost/h5/php/ckmodify.php",
              data:{id:e.target.dataset.id},
              success(res){
                //  console.log(res);
                 res = JSON.parse(res)
                 let num = document.getElementById("cka")
                 let name =document.getElementById("ckb")
                 let date = document.getElementById("ckc")
                 let state =document.getElementById("ckd")
                 let id = document.getElementById("ckxg")
                 let upd = document.getElementById("upd")
                 num.value = res.c_number
                 name.value = res.c_nameId
                 date.value = res.c_date
                 state.value = res.c_state
                 id.value = res.c_Id
                 upd.value = res.c_upd
              }
          })
          }
      })
     }

//实现添加
function addck(){
  // console.log(5);
  let num = document.getElementById("addnum").value
  let name =document.getElementById("addname").value
  let date = document.getElementById("adddate").value
  let state =document.getElementById("addstate").value
  // console.log(num,name,date,state)

  // // //非空判断
  if (!num || !name || !date || !state) return window.alert("请输入完整信息")
    console.log(num,name,date,state)
  // // //发送阿贾克斯
  Ajax({   
      url:"http://localhost/h5/php/cktj.php",
      type:"POST",
      data:{num,name,date,state},
      success(res){
        //  console.log(res)
        location.reload();
      }
  })
}
//确定完成修改出库信息
function ckmod(){
  let num = document.getElementById("cka").value
  let name =document.getElementById("ckb").value
  let date = document.getElementById("ckc").value
  let state =document.getElementById("ckd").value
  let Id = document.getElementById("ckxg").value
  let upd = document.getElementById("upd").value
  Ajax({
      url: "http://localhost/h5/php/ckUP.php",
      type: "POST",
      data: {num, name,date,state,Id,upd},
      success(res) {
          location.reload();
      }
  })
}

//渲染表格
function renderckInfo(result){
  let tbody = document.querySelector('tbody');
  tbody.innerHTML = ""
  result.forEach((item) => {   //遍历数据
    let str = `
    <tr>
    <td>${item.c_Id}</td>
    <td>${item.c_number}</td>
    <td>${item.c_nameId}</td>
    <td>${item.c_date}</td>
    <td>${item.c_state}</td>
    <td>
      <button type="button" class="btn btn-warning" data-o="update" data-bs-toggle="modal" data-bs-target="#ckUpdate" data-o='update'data-id='${item.c_Id}'>修改</button>
      <button type="button" class="btn btn-success" data-o="del"  data-id='${item.c_Id}'>删除</button>
    </td>
  </tr>
    `
    tbody.innerHTML += str;
})
}
//查询
function getGender(state){
  Ajax({
      url: "http://localhost/h5/php/ckxx.php",
      data:{state},
      success(res) {
          //覆盖数据
          sessionStorage.setItem("allckInfo",res)
          //转数据,渲染数据
          let result = JSON.parse(res).slice(0,9)
          renderckInfo(result);
      }
  })
}
//模糊查询
function getLike(){
  let name = document.getElementById("nameid").value
  //非空判断
  if(name.trim().length == 0){
      return window.alert("大哥,输入一点东西!")
  }
  //发送ajax
  Ajax({
      url: "http://localhost/h5/php/ckmm.php",
      data:{name},
      success(res) {
          //覆盖数据
          sessionStorage.setItem("allckInfo",res)
          //转数据,渲染数据
          let result = JSON.parse(res).slice(0,9)
          renderckInfo(result);
      }
  })
}
